<template>
	<view>
		<view style="text-align: center;margin-bottom: 20rpx;">
			已配对蓝牙设备
		</view>
		<view style="text-align: center;padding: 10rpx 0;border: 1rpx solid #2B85E4;border-radius: 15rpx;"
			class="uni-flex uni-row" v-for="(device,index) in deviceList" :key="index">
			<view style="width: 30rpx;">{{index+1}}</view>
			<view style="width: 180rpx;">{{device.name}}</view>
			<view style="width: 310rpx;">{{device.address}}</view>
			<view class="flex-item"><button type="primary" plain size="mini"
					@click="printSomething(device,printTest)">打印</button></view>
		</view>
		<u-empty v-if="!deviceList.length" text="没有搜索到蓝牙设备" mode="data"></u-empty>
		<view style="text-align: center;margin-top: 20rpx;">
			<u-button size="medium" @click="initPrinter()" type="primary" style="width: 40px;height: 30px;color: #fff;">
				刷新蓝牙设备
			</u-button>
		</view>

	</view>

</template>

<script>
	export default {
		props: {
			subdata: {
				typeof: Object,
				default: {
					BarRk: {
					},
					BarCk: {
					}

				},
				require: true
			},
		},
		data() {
			return {
				show: {
					setting: false
				},
				deviceList: [],
			}
		},
		onLoad(p) {
			// var that = this;

			// // #ifdef APP-PLUS
			// that.initPrinter();
			// // #endif
		},
		mounted() {
			var that = this;
			// #ifdef APP-PLUS
			that.initPrinter();
			// #endif
		},

		methods: {
			showPrinterList: function() {
				var that = this;
				that.show.setting = true;
				for (var i = 0; i < that.deviceList.length; i++) {
					if (that.deviceList[i].name == that.device.name) {
						that.deviceList[i].checked = true
					}
				}
			},
			initPrinter: function() {
				var that = this;
				that.deviceList = [];
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
				plus.android.importClass(BManager);
				var BAdapter = BManager.getAdapter();
				plus.android.importClass(BAdapter);
				var lists = BAdapter.getBondedDevices();
				plus.android.importClass(lists);
				var iterator = lists.iterator();
				plus.android.importClass(iterator);
				while (iterator.hasNext()) {
					var d = iterator.next();
					plus.android.importClass(d);
					var temp = {
						name: d.getName(),
						address: d.getAddress(),
						status: d.getBondState(),
						uuids: d.getUuids(),
						op: d
					};
					that.deviceList.push(temp);
				}
			},
			printSomething: function(dev, sb) {
				var that = this;
				var main = plus.android.runtimeMainActivity();
				var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
				var UUID = plus.android.importClass("java.util.UUID");
				var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
				var BAdapter = BluetoothAdapter.getDefaultAdapter();
				var device = BAdapter.getRemoteDevice(dev.address);
				plus.android.importClass(device);
				var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
				plus.android.importClass(bluetoothSocket);
				console.log("开始连接打印机:" + dev.name);
				if (!bluetoothSocket.isConnected()) {
					bluetoothSocket.connect();
					if (bluetoothSocket.isConnected()) {
						console.log("设备已连接,开始发送打印文件");
						var outputStream = bluetoothSocket.getOutputStream();
						plus.android.importClass(outputStream);
						sb(outputStream);
						bluetoothSocket.close();
						if (!bluetoothSocket.isConnected()) {
							console.log("设备已关闭");
						}
					} else {
						uni.showToast({
							title: '设备连接失败',
							icon: 'error',
							duration: 2000
						});
					}
				}
			},
			printTest: function(outputStream) {
				var that = this;
				for (let key in this.subdata) {
					if (this.subdata[key].BarCode.slice(0, 5) == '|Rd10') {
						var text = "! 5 800 592 576 1\r\n";
						text += "SPEED 1\r\n"; //打印速度 0 到 5
						//打印线条L
						text += "L 0 0 760 0 2\r\n"; 
						text += "L 0 500 760 500 2\r\n";
						text += "L 0 0 0 500 2\r\n";
						text += "L 760 0 760 500 2\r\n";

						text += "L 0 250 760 250 2\r\n";
						text += "L 80 0 80 500 2\r\n";
						text += "L 570 0 570 500 2\r\n";

						text += "L 380 0 380 250 2\r\n";
						text += "L 380 400 380 500 2\r\n";
						text += "L 460 400 460 500 2\r\n";

						text += "L 0 62 380 62 2\r\n";
						text += "L 0 124 380 124 2\r\n";
						text += "L 0 186 380 186 2\r\n";

						text += "L 0 400 760 400 2\r\n";
						
						// 打印文字T
						text += "T 3 0 15 22 编码\r\n";
						text += "T 3 0 15 84 名称\r\n";
						text += "T 3 0 15 146 规格\r\n";
						text += "T 3 0 15 208 图号\r\n";
						text += "T 3 0 15 300 流转\r\n";
						text += "T 3 0 15 330 卡\r\n";
						text += "T 3 0 15 442 批次\r\n";

						text += "T 3 0 395 442 人员\r\n";


						text += `T 3 0 95 22 ${this.subdata[key].cInvCode}\r\n`;
						text += `T 3 0 95 84 ${this.subdata[key].cInvName}\r\n`;
						text += `T 3 0 95 146 ${this.subdata[key].cInvStd}\r\n`;
						text += `T 3 0 95 208 ${this.subdata[key].cInvAddCode}\r\n`;
						
						let arr = this.subdata[key].DoCodeStr.split('|')
						if(arr.length == 3){
							text += `T 3 0 95 300 ${arr[0]}|${arr[1]}\r\n`;
							text += `T 3 0 95 330 ${arr[2]}\r\n`;
						}else{
							text += `T 3 0 95 300 ${this.subdata[key].DoCodeStr}\r\n`;
						}
						
						text += `T 3 0 95 442 ${this.subdata[key].cBatch}\r\n`;

						text += `T 3 0 475 442 ${this.subdata[key].cMaker}\r\n`;

						//二维码  
						// B横向打印  VB纵向打印
						text += "B QR 400 60 M 2 U 6\r\n";
						text += `MA,${this.subdata[key].BarCode}\r\n`;
						text += "ENDQR\r\n";

						text += "CENTER 760\r\n"; //居中 介绍点位置
						text += "SETBOLD 1\r\n"; //加粗 1加粗0不加粗
						text += "SETMAG 2 3\r\n"; //放大 宽度 高度  1-16
						text += `T 3 0 490 82 ${this.subdata[key].cInvMnemCode1}\r\n`;
						text += "SETMAG 0 0\r\n";
						text += "SETBOLD 0\r\n";

						text += "SETBOLD 2\r\n";
						text += "SETMAG 2 2\r\n";
						text += `T 3 0 490 300 ${this.subdata[key].cInvMnemCode2}\r\n`;
						text += "SETMAG 0 0\r\n";
						text += "SETBOLD 0\r\n";

						text += "SETBOLD 1\r\n";
						text += "SETMAG 2 3\r\n";
						// text += "CENTER 760\r\n";
						text += `T 3 0 490 415 ${this.subdata[key].iQuantity}${this.subdata[key].cComUnitName}\r\n`;
						text += "SETMAG 0 0\r\n";
						text += "SETBOLD 0\r\n";
						text += "FORM\r\n";
						text += "END\r\n";
						text += "PRINT\r\n"
						// console.log(text)
						var arrayBuffer = plus.android.invoke(text, 'getBytes', 'gbk');
						outputStream.write(arrayBuffer);
						outputStream.flush();
					} else {
						var text = "! 5 800 592 576 1\r\n";
						text += "SPEED 1\r\n";
						//打印线条
						text += "L 0 0 760 0 2\r\n";
						text += "L 0 500 760 500 2\r\n";
						text += "L 0 0 0 500 2\r\n";
						text += "L 760 0 760 500 2\r\n";

						text += "L 100 0 100 500 2\r\n";
						text += "L 510 0 510 500 2\r\n";

						text += "L 0 75 510 75 2\r\n";
						text += "L 0 150 510 150 2\r\n";
						text += "L 0 225 510 225 2\r\n";
						text += "L 0 300 760 300 2\r\n";
						text += "L 0 400 760 400 2\r\n";

						text += "T 3 0 18 28 供应商\r\n";
						text += "T 3 0 25 103 编码\r\n";
						text += "T 3 0 25 178 名称\r\n";
						text += "T 3 0 25 253 规格\r\n";
						text += "T 3 0 25 340 图号\r\n";
						text += "T 3 0 25 440 批次\r\n";

						text += `T 3 0 115 28 ${this.subdata[key].cVenCode}—${this.subdata[key].cVenName}\r\n`;
						text += `T 3 0 115 103 ${this.subdata[key].cInvCode}\r\n`;
						text += `T 3 0 115 178 ${this.subdata[key].cInvName}\r\n`;
						text += `T 3 0 115 253 ${this.subdata[key].cInvStd}\r\n`;
						text += `T 3 0 115 340 ${this.subdata[key].cInvAddCode}\r\n`;
						text += `T 3 0 115 440 ${this.subdata[key].cBatch}\r\n`;
 
						//二维码
						text += "B QR 535 55 M 2 U 8\r\n";
						text += `MA,${this.subdata[key].BarCode}\r\n`;
						text += "ENDQR\r\n";

						text += "CENTER 760\r\n";
						text += "SETBOLD 1\r\n";
						text += "SETMAG 2 2\r\n";
						text += `T 3 0 430 325 ${this.subdata[key].DMBarCode}\r\n`;
						text += "SETMAG 0 0\r\n";
						text += "SETBOLD 0\r\n";

						text += "SETBOLD 1\r\n";
						text += "SETMAG 2 3\r\n";
						text += `T 3 0 430 415 ${this.subdata[key].iQuantity}${this.subdata[key].cComUnitName}\r\n`;
						text += "SETMAG 0 0\r\n";
						text += "SETBOLD 0\r\n";
						text += "FORM\r\n";
						text += "END\r\n";
						text += "PRINT\r\n"
						var arrayBuffer = plus.android.invoke(text, 'getBytes', 'gbk');
						outputStream.write(arrayBuffer);
						outputStream.flush();
					}
				}

			},

			printBox: function(p, l, w, k, s) { //起点坐标、长高、宽、线宽、显示(上左下右)
				var text = "";
				if (s.t) {
					text = text.concat("L ", p.x, " ", p.y, " ", w, " ", p.y, " ", k, "\r\n");
				}
				if (s.l) {
					text = text.concat("L ", p.x, " ", p.y, " ", p.x, " ", p.y + l, " ", k, "\r\n");
				}
				if (s.b) {
					text = text.concat("L ", p.x, " ", p.y + l, " ", w, " ", p.y + l, " ", k, "\r\n");
				}
				if (s.r) {
					text = text.concat("L ", w, " ", p.y + l, " ", w, " ", p.y, " ", k, "\r\n");
				}
				return text;
			},
			cutLine: function(p, str) {
				var r = "";
				var max = 18;
				var n = parseInt(str.length / max);
				for (var i = 0; i < n; i++) {
					var temp = str.substr(i * max, max);
					r += "T 3 0 " + p.x + " " + (p.y + 40 * i) + " " + temp + "\r\n"
				}
				var w = str.substr(n * max);
				r += "T 3 0 " + p.x + " " + (p.y + 40 * n) + " " + w + "\r\n";
				return r;
			},
			printLineList: function(p, list) {
				var r = "";
				for (var i = 0; i < list.length && i < 5; i++) {
					r += "T 3 0 " + p.x + " " + (p.y + 40 * i) + " " + list[i] + "\r\n"
				}
				return r;
			},
			printLineList2: function(p, list) {
				var r = "";
				for (var i = 0; i < list.length && i < 10; i = i + 2) {
					r += "T 3 0 " + p.x + " " + (p.y + 60 * i) + " " + list[i] + "\r\n";
					if ((i + 1) < list.length) {
						r += "T 3 0 " + (p.x + 150) + " " + (p.y + 60 * i) + " " + list[i + 1] + "\r\n";
					}
				}
				return r;
			},
			mySleep: async function(time) {
				await this.mypromise(time);
			},
			mypromise: function(time) {
				return new Promise((resolve) => setTimeout(resolve, time));
			}
		}
	}
</script>

<style scoped>
	page {
		padding: 0;
		margin: 0;
	}

	.picked {
		background-color: lavender;
	}
</style>
